//
//  WKEmailInputView.m
//  InputWithEmailDemo
//
//  Created by 李家明 on 12-3-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "WKEmailInputView.h"

@implementation WKEmailInputView
@synthesize delegate;

#pragma mark - init

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImage *img = [UIImage imageNamed:@"bg_whiteframe.png"];
		UIImage *imgbg = [img stretchableImageWithLeftCapWidth:10 topCapHeight:5];
		UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height+5)];
		imgBack.image = imgbg;
		[self addSubview:imgBack];
		[imgBack release];
		
		mTableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 5, frame.size.width-10, frame.size.height-10) style:UITableViewStylePlain];
		mTableView.delegate = self;
		mTableView.dataSource = self;
        mTableView.tag = noDisableVerticalScrollTag;
		mTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
		mTableView.backgroundColor= [UIColor clearColor];
		mTableView.showsVerticalScrollIndicator = YES;
		mTableView.showsHorizontalScrollIndicator = NO;
		[self addSubview:mTableView];
        
        NSArray *ary  = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"EmailGroup" ofType:@"plist"]];
        filteredArray = [[NSMutableArray alloc] initWithArray:ary];
    }
    return self;
}

- (void)reloadTableView {
	[mTableView reloadData];
    [mTableView flashScrollIndicators];
	if ([filteredArray count] == 0) {
		[self removeFromSuperview];
	}
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    // Return the number of rows in the section.
    return [filteredArray count];
}


#pragma mark -
#pragma mark Table view delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleGray;
		cell.textLabel.font = [UIFont systemFontOfSize:16];
    }
    
	cell.textLabel.text = [filteredArray objectAtIndex:indexPath.row];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 30;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	NSString *str = [filteredArray objectAtIndex:indexPath.row];
	[delegate emailSeleted:str];
    [self removeFromSuperview];
}


#pragma mark - Memery Manager

- (void)dealloc {
	mTableView.delegate = nil;
	mTableView.dataSource = nil;
	[mTableView release],mTableView = nil;
	[filteredArray release],filteredArray = nil;
    [super dealloc];
}

#pragma mark - View Control 

- (void)setEmailInfo:(NSString*)text
{
    [filteredArray removeAllObjects];
    NSArray *ary  = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"EmailGroup" ofType:@"plist"]];
    for (NSString *emailstr in ary) {
        NSString  *email = [NSString stringWithFormat:@"%@%@",text,emailstr];
        [filteredArray addObject:email];
    }
    [self reloadTableView];
}

- (void)reSetEmailInfo:(NSString*)text
{
    [filteredArray removeAllObjects];
    NSArray *ary  = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"EmailGroup" ofType:@"plist"]];
    NSRange textRange = [text rangeOfString:@"@"];
    if (textRange.location != NSNotFound ) {
        textRange.length = [text length] - textRange.location;
    }
    else
    {
        textRange.location = 0;
    }
    NSString* findstr = [text substringWithRange:textRange];
    for (NSString *emailstr in ary) {
        NSRange emailstrRange = [emailstr rangeOfString:findstr];
        if (emailstrRange.location == NSNotFound) {
            continue;
        }
        
        NSString *subemailstr = [NSString stringWithString:[emailstr substringFromIndex:emailstrRange.location+emailstrRange.length]];
        NSString *email = [[NSString alloc] initWithFormat:@"%@%@",text,subemailstr];
        [filteredArray addObject:email];
        [email release];
    }
    [self reloadTableView];
}

- (void)closeEmailInfo
{
    [filteredArray removeAllObjects];
    [self reloadTableView];
}

@end
